﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Game_Hung_Trung
{
    
    public partial class Form1 : Form
    {
        public static PictureBox[,] picture = new PictureBox[5, 5];
        public static PictureBox[ ] convat = new PictureBox[3];
        public static int [] trungshit_roi = {0,2,4};
        public static Image[] trungshit=new Image[2];
        static Image rotrung = Image.FromFile("Ro.png");
        static int vitrigio = 0;
        static int giay = 0, giay2 = 0, giay3 = 0, giay4 = 0, giay5 = 0, giay6 = 0;
        static int vitritrungshit = 0;
        static int ts;
        static int ts1;
        static int ts2;
        static int count=0;
        
        public Form1()
        {
            InitializeComponent();
            // con vat
            #region 'con vật'
            convat[0] = pictureBox1;
            convat[1] = pictureBox2;
            convat[2] = pictureBox3;
            #endregion
            // vat roi va ro
            picture[0,0]=pictureBox4;
            picture[0,1]=pictureBox7;
            picture[0,2]=pictureBox10;
            picture[0,3]=pictureBox13;
            picture[2, 0] = pictureBox5; picture[2, 1] = pictureBox8; picture[2, 2] = pictureBox11; picture[2, 3] = pictureBox14;
            picture[4, 0] = pictureBox6; picture[4, 1] = pictureBox9; picture[4, 2] = pictureBox12; picture[4, 3] = pictureBox15;
            //Ro
            picture[0,4]=pictureBox16;
            picture[1,4] = pictureBox17;
            picture[2,4] = pictureBox18;
            picture[3,4] = pictureBox19;
            picture[4,4] = pictureBox20;

            

            /*for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    picture[i, j].Image = null;
                }
            }
            //picture[4,0].Image=//
              picture[1,4].Image=null; */
        }
        public void mang_picturebox()
        {
            for (int i = 0; i <= 5; i++)
            {
                for (int j = 0; j <= 5; j++)
                {
                    picture[i, j].Image=null;
                }
            }
        }

        public void tangtocroi()
        {
            try
            {
                if (count % 20 == 0 && count != 0)
                {
                    timer3.Interval -= 100;
                    timer4.Interval -= 100;
                    timer6.Interval -= 100;
                }
                if (count % 50 == 0 && count != 0)
                {
                    MessageBox.Show("Bạn đã Chiến Thằng");
                    return;
                }
            }catch(Exception){}
        }
        public void randomtrungshit()
        {
            trungshit[0]= Image.FromFile("egg.png");
            trungshit[1]=Image.FromFile("shit.png");
            Random rd = new Random();
            ts = rd.Next(0, 2);
            //vitritrungshit = rd.Next(0, 3);
            //picture[trungshit_roi[vitritrungshit],0].Image = trungshit[ts];
            timer3.Enabled = true;
        }
        public void randomtrungshit1()
        {
            trungshit[0] = Image.FromFile("egg.png");
            trungshit[1] = Image.FromFile("shit.png");
            Random rd = new Random();
            ts1 = rd.Next(0, 2);
            timer4.Enabled = true;
        }

        public void randomtrungshit2()
        {
            trungshit[0] = Image.FromFile("egg.png");
            trungshit[1] = Image.FromFile("shit.png");
            Random rd = new Random();
            ts2 = rd.Next(0, 2);
            timer6.Enabled = true;
        }

        public void trung_shitroi(int i)
        {
                
            
                picture[trungshit_roi[0], i].Image = trungshit[ts];

                for (int j = 0; j < 4; j++)
                {
                    if (j == i)
                    {
                        continue;
                    }
                    picture[trungshit_roi[vitritrungshit], j].Image = null;
                }
        }
        public void trung_shitroi1(int i)
        {
                picture[trungshit_roi[1], i].Image = trungshit[ts1];

                for (int j = 0; j < 4; j++)
                {
                    if (j == i)
                    {
                        continue;
                    }
                    picture[trungshit_roi[1], j].Image = null;
                }
        }
        public void trung_shitroi2(int i)
        {
                picture[trungshit_roi[2], i].Image = trungshit[ts2];

                for (int j = 0; j < 4; j++)
                {
                    if (j == i)
                    {
                        continue;
                    }
                    picture[trungshit_roi[2], j].Image = null;
                }
        }
        public void dichuyen()
        {
            Random rd = new Random();
            int so = 0;
            
                so = rd.Next(0, 2);
                
                Image img = convat[0].Image;
                convat[0].Image = convat[so+1].Image;
                convat[so+1].Image = img;
            
        } 
        public void hoanvi(ref PictureBox A,ref PictureBox B)
        { 
            PictureBox C;
            C = A;
            A = B;
            B = C; 
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            picture[vitrigio, 4].Image = Image.FromFile("Ro.png");

        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                string ktu = e.KeyData.ToString();
                if (e.KeyData == Keys.Enter)
                {
                    return;
                }
                if (e.KeyData == Keys.Left)
                {
                    vitrigio--;
                    if (vitrigio < 0) vitrigio = 0;

                    for (int i = 0; i < 5; i++)
                    {
                        picture[i, 4].Image = null;
                    }
                    picture[vitrigio, 4].Image = rotrung;
                    return;
                }
                if (e.KeyData == Keys.Right)
                {
                    vitrigio++;
                    if (vitrigio > 4) vitrigio = 4;

                    for (int i = 0; i < 5; i++)
                    {
                        if (i != vitrigio)
                        {
                            picture[i, 4].Image = null;
                        }
                    }
                    picture[vitrigio, 4].Image = rotrung;
                    return;
                }
                string tenfile = "";
                switch (ktu)
                {
                    case "G":
                        {
                            //
                            tenfile = "ga.png";

                            break;
                        }

                    case "V":
                        {
                            tenfile = "vit.png";
                            break;
                        }

                    case "N":
                        {
                            tenfile = "ngan.png";
                            break;
                        }
                    /*case "Left":
                        {
                            vitrigio--;
                            if (vitrigio < 0) vitrigio = 0;
                            picture[vitrigio, 4].Image = picture[4, 4].Image;
                            for (int i = 0; i < 5; i++)
                            {
                                if (i != vitrigio)
                                {
                                    picture[i, 4].Image = null;
                                }
                            }

                            break;
                        }*/
                    /* case "Right":
                         {
                             vitrigio++;
                             if (vitrigio > 4) vitrigio = 4;
                             picture[vitrigio, 4].Image = picture[4, 4].Image;
                             for (int i = 0; i > 5; i--)
                             {
                                 if (i != vitrigio)
                                 {
                                     picture[i, 4].Image = null;
                                 }
                             }
                             break;
                         }*/
                }

                if (convat[0].Image == null)
                {
                    convat[0].Image = Image.FromFile(tenfile);
                    //timer1.Enabled = true;
                    return;
                }
                if (convat[1].Image == null)
                {
                    convat[1].Image = Image.FromFile(tenfile);
                    //timer1.Enabled = true;
                    return;
                }
                if (convat[2].Image == null)
                {
                    convat[2].Image = Image.FromFile(tenfile);
                    timer1.Enabled = true;
                    timer2.Enabled = true;
                    timer5.Enabled = true;
                    return;
                }
                if (picture[0, 4].Image == null)
                {
                    picture[vitrigio, 4].Image = Image.FromFile("Ro.png");
                    return;
                }
            }
            catch (Exception) { }


        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            giay+=5000;
            if (giay % 5 == 0)
            {
                dichuyen();
                randomtrungshit();
            }
        }
        private void timer3_Tick(object sender, EventArgs e)
        {
            giay3 += 500;
            if (giay3 / 500 < 4)
            {
                trung_shitroi(giay3 / 500);
                return;
            }
            if (giay3 / 500 == 4)
            {
                picture[0, 3].Image = null;
                
            }
            if (trungshit[ts] == trungshit[0] && vitrigio == 0)
            {
                count++;
                lbldiem.Visible = true;
                lbldiem.Text = count.ToString();
            } 
            if (trungshit[ts] == trungshit[1] && vitrigio == 0)
            {
                count--;
                lbldiem.Visible = true;
                lbldiem.Text = count.ToString();
            }
            tangtocroi();
            timer3.Enabled = false;
            giay3 = 0;
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            giay2 += 5000;
            if (giay2 % 5 == 0)
            {
                randomtrungshit1();
            }
        }
        //static int d = 0;
        

        private void timer4_Tick(object sender, EventArgs e)
        {
            giay4 += 1000;
            if (giay4 / 1000 < 4)
            {
                trung_shitroi1(giay4 / 1000);
                    return;
            }
            if(giay4/1000==4)
            {
                picture[2, 3].Image = null;
            }
            if (trungshit[ts1] == trungshit[0] && vitrigio == 2)
            {
                count+=1;
                lbldiem.Visible = true;
                lbldiem.Text = count.ToString();
            }
            if (trungshit[ts1] == trungshit[1] && vitrigio == 2)
            {
                count--;
                lbldiem.Visible = true;
                lbldiem.Text = count.ToString();
            }
            timer4.Enabled = false;
            giay4 = 0;
        }

        private void timer5_Tick(object sender, EventArgs e)
        {
            giay5 += 5000;
            if (giay5 % 5 == 0)
            {
                randomtrungshit2();
            }
        }

        private void timer6_Tick(object sender, EventArgs e)
        {
            giay6 += 1000;
            if (giay6 / 1000 < 4)
            {
                trung_shitroi2(giay6 / 1000);
                return;
            }
            if (giay6 / 1000 == 4)
            {
                picture[4, 3].Image = null;
            }
            
            if (trungshit[ts2] == trungshit[0] && vitrigio == 4)// tuy theo cot ma gio dang o do.
            {
                // tang so lan pass
                count+=1;
                lbldiem.Visible = true;
                lbldiem.Text = count.ToString();
            }
            if (trungshit[ts2] == trungshit[1] && vitrigio == 4)
            {
                count--;
                lbldiem.Visible = true;
                lbldiem.Text = count.ToString();
            }
            timer6.Enabled = false;
            giay6 = 0;
        }
    }
}
